#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

/* change d size as needed */
int dimension = 1024;

int main(int argc, char *argv[])
{
	int d, i, j, k;
	double *A, *B, *C;
	struct timeval begin, end;

	if (argc == 2)
		dimension = atoi(argv[1]);

	printf("Dimension = %d\n", dimension);

	srand(292);
	int runs = 2;
	int r = runs;
	d = dimension;
	for ( ; r>=0; r--) {
		A = (double*)malloc(d*d*sizeof(double));
		B = (double*)malloc(d*d*sizeof(double));
		C = (double*)malloc(d*d*sizeof(double));

		for(i = 0; i < d; i++) {
			for(j = 0; j < d; j++) {
				A[d*i+j] = (rand()/(RAND_MAX - 1.0));
				B[d*i+j] = (rand()/(RAND_MAX - 1.0));
				C[d*i+j] = 0.0;
			}
		}
			
		gettimeofday(&begin, NULL);
		for(i = 0; i < d; i++) {
			for(j = 0; j < d; j++) {
				for(k = 0; k < d; k++) {
					C[d*i+j] += A[d*i+k] * B[d*k+j];
				}
			}
		}
		gettimeofday(&end, NULL);
		printf("[%d/%d] Multiplication loop took %ldus\t\n", (r+1), (runs+1), ((end.tv_sec * 1000000 + end.tv_usec) - (begin.tv_sec * 1000000 + begin.tv_usec)));
		
		free(A);
		free(B);
		free(C);
	}
	printf("\n");


	return 0;
}
